import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 创建相册对话框
class CreateAlbumDialog extends StatefulWidget {
  final String title;
  final String initialName;
  final String? initialDescription;
  final String submitButtonText;
  final Function(String name, String? description) onSubmit;

  const CreateAlbumDialog({
    Key? key,
    this.title = '创建相册',
    this.initialName = '',
    this.initialDescription,
    this.submitButtonText = '创建',
    required this.onSubmit,
  }) : super(key: key);

  @override
  State<CreateAlbumDialog> createState() => _CreateAlbumDialogState();
}

class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
  late TextEditingController _nameController;
  late TextEditingController _descriptionController;

  @override
  void initState() {
    super.initState();
    _nameController = TextEditingController(text: widget.initialName);
    _descriptionController =
        TextEditingController(text: widget.initialDescription ?? '');
  }

  @override
  void dispose() {
    _nameController.dispose();
    _descriptionController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(widget.title),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextField(
            controller: _nameController,
            decoration: const InputDecoration(
              labelText: '相册名称',
              hintText: '请输入相册名称',
            ),
            autofocus: true,
            textInputAction: TextInputAction.next,
          ),
          SizedBox(height: 16.h),
          TextField(
            controller: _descriptionController,
            decoration: const InputDecoration(
              labelText: '相册描述（可选）',
              hintText: '请输入相册描述',
            ),
            maxLines: 2,
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text('取消'),
        ),
        ElevatedButton(
          onPressed: () {
            final name = _nameController.text.trim();
            final description = _descriptionController.text.trim();
            if (name.isEmpty) {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('相册名称不能为空')),
              );
              return;
            }
            widget.onSubmit(name, description.isNotEmpty ? description : null);
          },
          child: Text(widget.submitButtonText),
        ),
      ],
    );
  }
}
